package com.ssb.plugins;

import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.exception.BusinessException;
import com.apache.ius.common.CommonUtils;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.DataMap;
import com.apache.tools.DateUtils;
import com.apache.tools.NumberUtils;
import com.apache.tools.StrUtil;
import org.apache.log4j.Logger;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * @Classname EditUserBalancePluginImpl
 * @Description 更新佣金余额表
 * @Date 2020/2/17 20:44
 * @Created by fpc
 */
public class EditUserBalancePluginImpl implements CustomMethodPlugin {
	private Logger log = Logger.getLogger(getClass());

	@Override
	public Object beforeInvoke(Map<String, Object> param) {
		log.info("start->> 执行插件事前方法....");
		String type = String.valueOf(param.get("type"));
		String applyEname = String.valueOf(param.get("ssCashApply.applyEname"));
		String applyUserId = String.valueOf(param.get("ssCashApply.applyUserId"));
		double money = Double.valueOf(String.valueOf(param.get("ssCashApply.money")));//申请提现金额
		if(Validator.isNull(type)||Validator.isNull(applyEname)||Validator.isNull(String.valueOf(money))){
			throw new BusinessException("【type】或者【applyEname】或者【money】或者【applyUserId】为空，请检验数据。");
		}
		Map<String,Object> appConfigCache = (Map<String,Object>) CommonUtils.getCacheByAppConfig("yjpz");
		double outMax = 10000.00;
		double outMin = 100.00;
		if(appConfigCache != null && !appConfigCache.isEmpty()){
			String valstr = StrUtil.doNull(String.valueOf(((Map)appConfigCache.get("yjpz_out_max")).get("appValue")), "10000.00");
			String valLowstr = StrUtil.doNull(String.valueOf(((Map)appConfigCache.get("yjpz_out_max")).get("appLowValue")), "100.00");
			outMax = NumberUtils.getDouble(valstr, outMax, true);
			outMin = NumberUtils.getDouble(valLowstr, outMin, true);
		}
		if(NumberUtils.subtract(money,outMax)>0){
			throw new BusinessException("提现金额不能大于"+outMax+"元，请重新输入。");
		}
		if(NumberUtils.subtract(money,outMin)<0){
			throw new BusinessException("提现金额不能小于"+outMin+"元，请重新输入。");
		}
		Map whereMap = new HashMap();
		whereMap.put("ssUserBalance.w_userEname",applyEname);
		ResultEntity resultEntity = IusPluginUtil.getInstance().select("obj", "s_ssUserBalance", whereMap);
		DataMap ssUserBalanceMap = (DataMap) resultEntity.getEntity();
		Map<String,Object> saveMap = new HashMap<>();
		saveMap.put("ssUserBalance.w_userEname",applyEname);
		String availableBalance = String.valueOf(ssUserBalanceMap.get("availableBalance"));//可用余额
		String freezeBalance = String.valueOf(ssUserBalanceMap.get("freezeBalance"));//冻结余额
		String totalCashOut = String.valueOf(ssUserBalanceMap.get("totalCashOut"));//提现总额
		double availableBalanceDouble = NumberUtils.getDouble(availableBalance, 0.00, true);
		if(NumberUtils.subtract(money,availableBalanceDouble)>0){
            throw new BusinessException("提现金额不能大于剩余金额，请重新输入。");
        }
		double freezeBalanceDouble = NumberUtils.getDouble(freezeBalance, 0.00, true);
		double totalCashOutDouble = NumberUtils.getDouble(totalCashOut, 0.00, true);
		String newTotalCashOut=null;
		String newFreezeBalance=null;
		String newAvailableBalance=null;
		if("add".equals(type)){
			whereMap = new HashMap();
			whereMap.put("uctUser.w_userId",applyUserId);
			whereMap.put("result_column","uctUser.userTitle,uctUser.cardNum,uctUser.cardType");
			/**
			 * "uctUser.userTitle":"开户人姓名",
			 * "uctUser.cardNum":"银行卡号",
			 * "uctUser.cardType":"开户银行"
			 */
			ResultEntity userResultEntity = IusPluginUtil.getInstance().select("plateform","obj", "s_uctUser", whereMap);
			DataMap dataMap = (DataMap)userResultEntity.getEntity();
			if(!Validator.isEmpty(dataMap)) {
				param.put("ssCashApply.applyCardName",dataMap.get("userTitle"));//开户人姓名
				param.put("ssCashApply.applyCardType",dataMap.get("cardType"));//开户银行
				param.put("ssCashApply.applyCardNum",dataMap.get("cardNum"));//银行卡号
				param.put("ssCashApply.applyCertNo",dataMap.get("certNo"));//身份证号
			}
			param.put("ssCashApply.sysName",DateUtils.Now.fmt_yyyyMM());//周期
			newFreezeBalance = String.valueOf(NumberUtils.add(freezeBalanceDouble,money));
			newAvailableBalance = String.valueOf(NumberUtils.subtract(availableBalanceDouble,money));
			saveMap.put("ssUserBalance.availableBalance",newAvailableBalance);//new可用余额
			saveMap.put("ssUserBalance.freezeBalance",newFreezeBalance);//new冻结余额
		}else if("audit".equals(type)){
			String nowTime = DateUtils.getNow();
			param.put("ssCashApply.modifyTime",nowTime);
			String applyStatus = String.valueOf(param.get("ssCashApply.applyStatus"));
			newFreezeBalance = String.valueOf(NumberUtils.subtract(freezeBalanceDouble,money));
			if("90".equals(applyStatus)){//审核通过
				newTotalCashOut = String.valueOf(NumberUtils.add(totalCashOutDouble,money));
				saveMap.put("ssUserBalance.totalCashOut",newTotalCashOut);
			}else{//审核不通过
				newAvailableBalance = String.valueOf(NumberUtils.add(availableBalanceDouble,money));
				saveMap.put("ssUserBalance.availableBalance",newAvailableBalance);
			}
			saveMap.put("ssUserBalance.freezeBalance",newFreezeBalance);
		}
		ResultEntity resultEntity1 = IusPluginUtil.getInstance().saveInvoke("u_ssUserBalance", saveMap, true);
		return param;
	}

	@Override
	public Object doInvoke(Map<String, Object> param) {
		log.info("start->> 执行插件方法....");

		return null;
	}

	@Override
	public Object afterInvoke(Map<String, Object> param) {
		log.info("start->> 执行插件事后方法....");

		return null;
	}

	/**
	 * 规则校验统一入口
	 * @param paramsVo
	 */
	public void run(ParamsVo paramsVo) {
		beforeInvoke(paramsVo.getParams());
	}



}

